package com.chat.api.web.service;

import com.chat.api.common.exception.ApiException;
import com.chat.api.exception.CoreExceptionEnum;
import com.chat.api.utils.ResultUtil;
import com.chat.api.utils.util.IdWorkerUtil;
import com.chat.api.utils.util.ParamUtil;
import com.chat.api.web.dao.RemarkRepo;
import com.chat.api.web.po.RemarkBean;
import com.chat.api.web.po.UserBean;
import com.chat.api.web.vo.MessageEntity;
import com.chat.api.web.vo.RemarkEntity;
import io.swagger.annotations.Api;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Service
public class RemarkService {

    @Autowired
    private RemarkRepo remarkRepo;
    @Autowired
    private UserService userService;
    @Autowired
    private BlogService blogService;

    /**
     * 获取博客评论列表
     * @param id
     * @param blogId
     * @param request
     * @return
     */
    public List<List> getList(Long id, Long blogId, HttpServletRequest request) {
        try {
            List<List> lists = new ArrayList<>();
            List<RemarkBean> all = Lists.newArrayList();
            HttpSession session = request.getSession();
            String userId = (String) session.getAttribute(session.getId());
            Long userIdL = ParamUtil.getLongParameter(userId, -1L);
            UserBean userBean = userService.findById(userIdL);
            // TODO 博客评论内容获取
            if (blogId < 0) {
                all = remarkRepo.findAllSort();
            }else {
                all = remarkRepo.findAllSortByBlogId(blogId);
            }
            for (RemarkBean bean : all) {
                List<RemarkEntity> remarkBeans = new ArrayList<>();
                List<RemarkEntity> list = findRemarkList(bean,remarkBeans);
                RemarkEntity remarkEntity = new RemarkEntity();
                remarkEntity.setValue(bean);
                list.add(remarkEntity);
                Collections.sort(list, new Comparator<RemarkEntity>() {
                    @Override
                    public int compare(RemarkEntity o1, RemarkEntity o2) {
                        int flag = o1.getRDate().compareTo(o2.getRDate());
                        return flag;
                    }
                });
                lists.add(list);
            }
            return lists;
        }catch (Exception e){
            e.printStackTrace();
            throw new ApiException("获取评论列表失败！");
        }
    }

    //递归获取子评论
    public List<RemarkEntity> findRemarkList(RemarkBean bean,List<RemarkEntity> remarkBeans) {
        try {
            Long id = bean.getId();
            List<RemarkBean> list = remarkRepo.findRemarkList(id);
            if (list.size()>0){
                for (RemarkBean remarkBean : list) {
//                    for (RemarkBean bean1 : list) {
                        RemarkEntity remarkEntity = new RemarkEntity();
                        remarkEntity.setValue(remarkBean);
                        remarkBeans.add(remarkEntity);
//                    }
                    findRemarkList(remarkBean,remarkBeans);
                }
            }
            return remarkBeans;
        }catch (Exception e){
            e.printStackTrace();
            throw new ApiException("获取子评论失败！！！");
        }
    }

    /**
     * 保存评论
     * @param id
     * @param remarkBean
     * @return
     */
    @Transactional(readOnly = false)
    public String save(Long userId, RemarkBean remarkBean) {
        try {
            UserBean bean = userService.findById(userId);
            if (bean == null){
                return "用户不存在，请注册！";
            }
            remarkBean.setUsername(bean.getName());
            remarkBean.setRDate(new Date());
            Long id2 = IdWorkerUtil.getInstance().nextId();
            remarkBean.setId(id2);
            remarkBean.setCreateBy(bean.getId());
            remarkBean.setCreateDate(new Date());
            remarkBean.setUpdateBy(bean.getId());
            remarkBean.setUpdateDate(new Date());
            remarkRepo.save(remarkBean);
            Long blogId = remarkBean.getBlogId();
            if (blogId != null && blogId != 0){
                blogService.saveRemarkCount(blogId);
            }
            return "ok";
        }catch (Exception e){
            e.printStackTrace();
            return "评论保存失败！";
        }
    }

    /**
     * 获取博客主页评论数量
     * @return
     */
    public MessageEntity getBlogRemarkCount() {
        try {
            Integer count = remarkRepo.getBlogRemarkCount();
            return ResultUtil.success(count);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.fail("博客评论数量获取失败！！！");
        }
    }

}
